import { mapMutations, mapActions, mapState } from "vuex";
import _ from "lodash";
import { typeClassName } from "@/pages/config/classNameMap";
import dayjs from "dayjs";
export default {
  data() {
    return {
      vms: {}, //页面组件vue实例
      models: {
        //模型
        equipTechnologyParam: {
          ...typeClassName["equipTechnologyParam"],
        },
        qualityItemType: {
          ...typeClassName["qualityItemType"],
        },
        paramStatus: {
          ...typeClassName["paramStatus"],
        },
        equipParamCategory: {
          ...typeClassName["equipParamCategory"],
        },
      },
      onlyRead: false,
      paramTypeOptions: [],
      statusOptions:[],
      dataSet: {
        //数据集
        tableData: [],
        formData: {},
      },
    };
  },
  provide() {
    return {
      anc: this,
    };
  },
  computed: {},
  created() {
    Object.keys(this.models).map((k) => {
      this.models[k].clz = this.$modelObject.create(this.models[k].modelType);
      this.models[k].clzs = this.$modelObject.create(
        this.models[k].modelType + "Collection"
      );
    });
  },
  mounted() {
    this.$nextTick(() => {
      let paramFilter = {
        className: this.models.qualityItemType.className,
        type: this.models.qualityItemType.type,
      };
      this.getOptions(paramFilter, this.models.qualityItemType.clz).then(
        (res) => {
          this.paramTypeOptions = res;
        }
      );
      // 参数状态
      let statusFilter = {
        className: this.models["paramStatus"].className,
        type: this.models["paramStatus"].type,
      };
      this.getOptions(statusFilter, this.models.paramStatus.clz).then((res) => {
        this.statusOptions = res;
      });
      if (process.env.NODE_ENV === "development") {
        console.log(this);
      }
    });
  },
  methods: {
    /**
     *
     * @param {分页} pageNavigation
     * @param {筛选条件} filter
     * @param {*} orderBy
     * 页面加载方法
     */
    loadPage(pageNavigation, filter, orderBy = {}, like = false) {
      let newFilter = {
        ...filter,
        className: this.models.equipTechnologyParam.className,
        type: this.models.equipTechnologyParam.type,
      };
      this.vms.equipTechnologyParamTable.loading = true;
      this._$queryByPage(
        this.models["equipTechnologyParam"].clzs,
        pageNavigation,
        newFilter,"","",false
      ).then((res) => {
        this.vms.equipTechnologyParamTable.loading = false;
        this.dataSet.tableData = []
         this.models[
          "equipTechnologyParam"
        ].clzs.valueRecords[0] .hier.content.forEach((d) => {
          if (d.name != "根节点") {
            this.dataSet.tableData.push({
              ...d,
              ...d.values,
            });
          }
        });
        this.vms.equipTechnologyParamTable.pageNavigation.total =
          this.models["equipTechnologyParam"].clzs.total - 1;
      });
    },

    /**
     * 人员管理新增
     */
    handleCreate() {
      this.vms.createModal.show = true;
      this.onlyRead = false;
      this.dataSet.formData = this.$modelObject.getTpl(
        this.models.equipTechnologyParam.modelType
      ); // 这是一个整理接口数据JSON格式的方法
    },
    /**
     *
     * @param {*} row
     * 人员管理查看
     */
    handleDetail(row) {
      console.log(row);
      this.vms.detailModal.show = true;
      this.dataSet.formData = row;
      this.onlyRead = true;
    },
    /**
     *
     * @param {*} row
     * 人员管理编辑
     */
    handleEdit(row) {
      this.vms.editModal.show = true;
      this.onlyRead = false;
      this.dataSet.formData = row;
    },
    /**
     *
     * @param {行数据} row
     * @param {索引} index
     * 人员管理删除数据
     */
    handleDelete(row, index) {
      let _this = this;
      this._$deleteConfirm(() => {
        this.$api.HierInstanceDeleteHier([row.id]).then((flag) => {
          this.vms.equipTechnologyParamTable.pageNavigation.pageNumber = 1;
          this.loadPage(
            this.vms.equipTechnologyParamTable.pageNavigation,
            {},
            "",
            "",
            false
          );
        });
      });
     
    },
    /**
     *
     * @param {行数据} selection
     */
    handleDeleteMultiple(selection) {
      let ids = selection.map((d) => d.id);
      this._$deleteConfirm(() => {
        this.$api.HierInstanceDeleteHier(ids).then((flag) => {
          this.vms.equipTechnologyParamTable.pageNavigation.pageNumber = 1;
          this.loadPage(
            this.vms.equipTechnologyParamTable.pageNavigation,
            {},
            "",
            "",
            false
          );
        });
      });
    },

    // 创建人员管理提交
    createModalOk() {
      // this.vms.createView.validate((valid) => {
      this._$validateAll([this.vms.createView], (valid) => {
       
        if (valid) {
          let newData = {
            values: this.dataSet.formData.values,
            containerModel: this.models.equipTechnologyParam.modelType,
            containerId: this.models.equipTechnologyParam.clzs.valueRecords[0].id,
            parentId: "root",
          };

          this.$api.HierInstanceSaveHier([newData]).then((flag) => {
            this.vms.equipTechnologyParamTable.pageNavigation.pageNumber = 1;
            this.loadPage(this.vms.equipTechnologyParamTable.pageNavigation, {}, "", "", false);
            this.vms.createModal.show = false;
          });
        } else {
          this.$Message.info("请填写必填内容");
        }
    });
    },

    editModalOk() {
      this.vms.editView.validate((valid) => {
        if (valid) {
          let newData = {
            values: this.dataSet.formData.values,
            containerModel: this.models.equipTechnologyParam.modelType,
            containerId: this.models.equipTechnologyParam.clzs.valueRecords[0].id,
            parentId: "root",
            id:this.dataSet.formData.id
          };

          this.$api.HierInstanceSaveHier([newData]).then((flag) => {
            this.vms.equipTechnologyParamTable.pageNavigation.pageNumber = 1;
            this.loadPage(this.vms.equipTechnologyParamTable.pageNavigation, {}, "", "", false);
            this.vms.editModal.show = false;
          });
        } else {
          this.$Message.info("请填写必填内容");
        }
      });
    },
    // 获取下拉框
    getOptions(filter, classObject) {
      let newFilter = "";
      if (this.tools.isReal(filter)) {
        newFilter = classObject.util.getFilter(filter);
      }
      let statusOptions = [];
      return classObject.query(newFilter).then((flag) => {
        if (flag) {
          statusOptions = classObject.record.hier.content.map((d) => {
            return {
              label: d.title === null ? "" : d.title,
              value: d.name === null ? "" : d.name,
            };
          });
        } else {
          statusOptions = [];
        }
        return statusOptions;
      });
    },
    setOptionsByHier(
      classMapName,
      classObject,
      optionName,
      optionConfig = { label: "sf_name", value: "sf_code" },
      ref
    ) {
      let filter = {
        className: typeClassName[classMapName].className,
        type: typeClassName[classMapName].type,
      };
      let newFilter = "";
      if (this.tools.isReal(filter)) {
        newFilter = classObject.util.getFilter(filter);
      }
      let statusOptions = [];
      return classObject.query(newFilter).then((flag) => {
        if (flag ) {
          statusOptions = classObject.record.hier.content.map((d) => {
            let option = {};
            for (let key in optionConfig) {
              option[key] = d.values[optionConfig[key]];
            }
            return option;
          });
        } else {
          statusOptions = [];
        }
        this[optionName] = statusOptions;
      });
    },
  },
};
